﻿Public Class HDCurveLimitCfg

#Region " 成员变量 "

    Private _pi As ProjectItem
    Private _rti As RefTypeItem

#End Region

#Region " 属性 "

    Public Property RefTypeItem() As RefTypeItem
        Get
            Return _rti
        End Get
        Set(ByVal value As RefTypeItem)
            _rti = value
        End Set
    End Property

#End Region

#Region " 构造函数 "

    Public Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。

    End Sub

    Public Sub New(ByVal rti As RefTypeItem)

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()

        ' 在 InitializeComponent() 调用之后添加任何初始化。
        _rti = rti
    End Sub

#End Region
    
    Private Sub HDCurveLimitCfg_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        _pi = MainForm.pi
        'Dim showTempUp, showTempDown, showHumiUp, showHumiDown As Boolean


        Me.nudTempLower.Value = _rti.HDCurveTempDown
        Me.nudTempUpper.Value = _rti.HDCurveTempUp
        Me.nudHumiLower.Value = _rti.HDCurveHumiDown
        Me.nudHumiUpper.Value = _rti.HDCurveHumiUp

        '**************************************************************
        '20090616 李吉鹏增加 为增加温湿度基准线及控制显示功能
        Me.nudTempLineUp.Value = _rti.HDCurveTempLineUp
        Me.nudTempLineDown.Value = _rti.HDCurveTempLineDown
        Me.nudHumiLineUp.Value = _rti.HDCurveHumiLineUp
        Me.nudHumiLineDown.Value = _rti.HDCurveHumiLineDown



        Me.TempUpYes.Checked = _rti.HDCurveTempLineUpShow
        Me.TempUpNo.Checked = Not _rti.HDCurveTempLineUpShow

        If Not _rti.HDCurveTempLineUpShow Then
            Me.nudTempLineUp.ReadOnly = True
        Else : Me.nudTempLineUp.ReadOnly = False
        End If
        Me.TempDownYes.Checked = _rti.HDCurveTempLineDownShow
        Me.TempDownNo.Checked = Not _rti.HDCurveTempLineDownShow
        If Not _rti.HDCurveTempLineDownShow Then
            Me.nudTempLineDown.ReadOnly = True
        Else : Me.nudTempLineDown.ReadOnly = False
        End If
        Me.HumiUpYes.Checked = _rti.HDCurveHumiLineUpShow
        Me.HumiUpNo.Checked = Not _rti.HDCurveHumiLineUpShow
        If Not _rti.HDCurveHumiLineUpShow Then
            Me.nudHumiLineUp.ReadOnly = True
        Else : Me.nudHumiLineUp.ReadOnly = False
        End If
        Me.HumiDownYes.Checked = _rti.HDCurveHumiLineDownShow
        Me.HumiDownNo.Checked = Not _rti.HDCurveHumiLineDownShow
        If Not _rti.HDCurveHumiLineDownShow Then
            Me.nudHumiLineDown.ReadOnly = True
        Else : Me.nudHumiLineDown.ReadOnly = False
        End If
        '*********************************************************************************

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If CheckValidate() = False Then
            Exit Sub
        End If
        If Save() = True Then
            'msg.Show("保存成功。")
            Me.DialogResult = Windows.Forms.DialogResult.OK
        Else
            msg.ShowError("设置失败。")
        End If
    End Sub
    '**************************************************************
    '20090616 李吉鹏增加 为增加温湿度基准线及控制显示功能
    Private Function CheckValidate() As Boolean
        '检查输入是否正确
        If Me.TempUpYes.Checked Then
            If Me.nudTempLineUp.Value > Me.nudTempUpper.Value Then
                msg.ShowWarning("温度基准线不能高于温度最高值")
                Return False
            End If
        End If
        If Me.TempDownYes.Checked Then
            If Me.nudTempLineDown.Value < Me.nudTempLower.Value Then
                msg.ShowWarning("温度基准线不能低于温度最低值")
                Return False
            End If
        End If
        If Me.HumiUpYes.Checked Then
            If Me.nudHumiLineUp.Value > Me.nudHumiUpper.Value Then
                msg.ShowWarning("湿度基准线不能高于湿度最高值")
                Return False
            End If
        End If
        If Me.HumiDownYes.Checked Then
            If Me.nudHumiLineDown.Value < Me.nudHumiLower.Value Then
                msg.ShowWarning("湿度基准线不能低于温度最低值")
                Return False
            End If
        End If

        If Me.TempUpYes.Checked AndAlso Me.TempDownYes.Checked Then
            If Me.nudTempLineUp.Value < Me.nudTempLineDown.Value Then
                msg.ShowWarning("温度上限不能低于下限")
                Return False
            End If
        End If
        If Me.HumiUpYes.Checked AndAlso Me.HumiDownYes.Checked Then
            If Me.nudHumiLineUp.Value < Me.nudHumiLineDown.Value Then
                msg.ShowWarning("湿度上限不能低于下限")
                Return False
            End If
        End If

        Return True
    End Function
    '*********************************************************************

    Private Function Save() As Boolean

        Try

            _rti.HDCurveTempDown = Me.nudTempLower.Value
            _rti.HDCurveTempUp = Me.nudTempUpper.Value
            _rti.HDCurveHumiDown = Me.nudHumiLower.Value
            _rti.HDCurveHumiUp = Me.nudHumiUpper.Value

            '**************************************************************
            '20090616 李吉鹏增加 为增加温湿度基准线及控制显示功能
            _rti.HDCurveTempLineUp = Me.nudTempLineUp.Value
            _rti.HDCurveTempLineDown = Me.nudTempLineDown.Value
            _rti.HDCurveHumiLineUp = Me.nudHumiLineUp.Value
            _rti.HDCurveHumiLineDown = Me.nudHumiLineDown.Value


            If Me.TempUpYes.Checked Then
                _rti.HDCurveTempLineUpShow = True
            Else : _rti.HDCurveTempLineUpShow = False
            End If

            If Me.TempDownYes.Checked Then
                _rti.HDCurveTempLineDownShow = True
            Else : _rti.HDCurveTempLineDownShow = False
            End If

            If Me.HumiUpYes.Checked Then
                _rti.HDCurveHumiLineUpShow = True
            Else : _rti.HDCurveHumiLineUpShow = False
            End If

            If Me.HumiDownYes.Checked Then
                _rti.HDCurveHumiLineDownShow = True
            Else : _rti.HDCurveHumiLineDownShow = False
            End If
           
            '***************************************************************
            Save = RefTypeService.Update4HDCurve(_rti) > 0

        Catch ex As Exception
            Debug.Write(ex.Message)
            Save = False
        End Try

    End Function

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub nudTempLower_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudTempLower.ValueChanged

        Me.nudTempUpper.Minimum = Me.nudTempLower.Value + 1

    End Sub

    Private Sub nudTempUpper_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudTempUpper.ValueChanged

        Me.nudTempLower.Maximum = Me.nudTempUpper.Value - 1

    End Sub

    Private Sub nudHumiLower_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudHumiLower.ValueChanged

        Me.nudHumiUpper.Minimum = Me.nudHumiLower.Value + 1

    End Sub

    Private Sub nudHumiUpper_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nudHumiUpper.ValueChanged

        Me.nudHumiLower.Maximum = Me.nudHumiUpper.Value - 1

    End Sub

    Private Sub GroupBox3_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox3.Enter

    End Sub

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TempUpYes.CheckedChanged
        Me.nudTempLineUp.ReadOnly = False

    End Sub

    Private Sub HumiDownNo_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HumiDownNo.CheckedChanged
        Me.nudHumiLineDown.ReadOnly = True
    End Sub

    Private Sub HumiDownYes_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HumiDownYes.CheckedChanged
        Me.nudHumiLineDown.ReadOnly = False
    End Sub

    Private Sub TempDownNo_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TempDownNo.CheckedChanged
        Me.nudTempLineDown.ReadOnly = True
    End Sub

    Private Sub TempUpNo_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TempUpNo.CheckedChanged
        Me.nudTempLineUp.ReadOnly = True
    End Sub

    Private Sub TempDownYes_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TempDownYes.CheckedChanged
        Me.nudTempLineDown.ReadOnly = False
    End Sub

    Private Sub HumiUpYes_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles HumiUpYes.CheckedChanged
        Me.nudHumiLineUp.ReadOnly = False

    End Sub

    Private Sub HumiUpNo_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles HumiUpNo.CheckedChanged
        Me.nudHumiLineUp.ReadOnly = True
    End Sub
End Class